<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Orders_model  extends MY_Model  {

    public $table = 'orders';

	public function __construct() {
		$this->class_name = get_class ( $this );
		parent::__construct( $this->class_name, $this->table );
		parent::init( $this->class_name, $this->table );
	}
	
	function getAllOrders($id){
		$sql = "SELECT orders.* ,
			billing.city as b_city,
			b_countries.name as b_country,
			b_countries.vat as b_country_vat,
			billing.address as b_address,
			billing.country_id as b_country_id,
			billing.first_name as b_first_name,
			billing.last_name as b_last_name,
			billing.natural_person as natural_person,
			shipping.city as s_city,
			s_countries.name as s_country,
			shipping.address as s_address,
			shipping.first_name as s_first_name,
			shipping.last_name as s_last_name
			FROM orders 
			LEFT JOIN order_address as billing ON billing.id = orders.billing_id 
			LEFT JOIN countries as b_countries ON b_countries.id = billing.country_id 
			LEFT JOIN order_address as shipping ON shipping.id = orders.shipping_id 
			LEFT JOIN countries as s_countries ON s_countries.id = shipping.country_id 
			WHERE orders.id = ?";
				
		$query = $this->db->query($sql, array($id));
		
		return $query->result_object();
	}
	
	public function getLastOrder($iduser) {
		return $this->db->select()->from($this->table)->where(array('user_id' => $iduser) )->limit(1)->order_by("id", "desc")->get()->result();
	}
	
}